<?php

namespace App\Console\Commands;

use App\Library\Logic\ShopImageLogic;
use App\Library\Logic\ShopProductLogic;
use App\Library\Logic\UploadFileLogic;
use Exception;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Model;

class ShopProductFileCommand extends Command
{
    /**
     * 处理指定商品图片
     *
     * @var string
     */
    protected $signature = 'command:shopProductFile {serialNumber}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * handle
     * @throws Exception
     */
    public function handle(): void
    {
        $serialNumber = $this->argument('serialNumber');
        if (empty($serialNumber) || strlen($serialNumber) !== 20) return;
        $shopProduct = ShopProductLogic::findBySerialNumber($serialNumber);
        if (is_null($shopProduct)) return;
        $this->handleMain($shopProduct);
        $this->handleDesc($shopProduct);
        $this->handleVideo($shopProduct);
        $shopProduct->save();
        mq('shopProductChange', $shopProduct);
    }

    /**
     * handleMain
     * @param Model $shopProduct
     * @return void
     */
    private function handleMain(Model $shopProduct): void
    {
        $where = ['imgType' => 1, 'imgSourceType' => 4, 'imgSourceId' => $shopProduct->product_id];
        if (is_null($shopProduct->main_img_ids)) {
            ShopImageLogic::updateByWhereOfIds($where);
            $shopProduct->product_avatar = null;
            $shopProduct->product_avatar_img_id_json = null;
            return;
        }
        $ids = [];
        foreach ($shopProduct->main_img_ids as $k => $v) {
            $ids = $this->handleImage($shopProduct, $where, $k, $v, $ids);
        }
        ShopImageLogic::updateByWhereOfIds($where, $ids);
        $shopProduct->product_avatar = $shopProduct->main_img_ids[0];
        if (strlen($shopProduct->product_avatar) === 24) {
            $shopProduct->product_avatar = str_replace('/', '', $shopProduct->product_avatar);
        }
        $shopProduct->product_avatar_img_id_json = json_encode($ids);
    }

    /**
     * handleDesc
     * @param Model $shopProduct
     * @return void
     */
    private function handleDesc(Model $shopProduct): void
    {
        $where = ['imgType' => 1, 'imgSourceType' => 5, 'imgSourceId' => $shopProduct->product_id];
        if (is_null($shopProduct->desc_img_ids)) {
            ShopImageLogic::updateByWhereOfIds($where);
            $shopProduct->product_content_img_id_json = null;
            return;
        }
        $ids = [];
        foreach ($shopProduct->desc_img_ids as $k => $v) {
            $ids = $this->handleImage($shopProduct, $where, $k, $v, $ids);
        }
        ShopImageLogic::updateByWhereOfIds($where, $ids);
        $shopProduct->product_content_img_id_json = json_encode($ids);
    }

    /**
     * handleVideo
     * @param Model $shopProduct
     * @return void
     */
    private static function handleVideo(Model $shopProduct): void
    {
        $where = [
            'imgType' => 2,
            'imgSourceType' => 4,
            'imgSourceId' => $shopProduct->product_id,
        ];
        if (is_null($shopProduct->video_id)) {
            ShopImageLogic::updateByWhereOfIds($where);
            return;
        }
        $shopImage = ShopImageLogic::findByWhere($where);
        $ids = [0];
        if (is_null($shopImage)) {
            $uploadFile = UploadFileLogic::findBySerialNumber($shopProduct->video_id);
            if (is_null($uploadFile)) return;
            $shopImage = ShopImageLogic::insertForModel(array_merge($where, [
                'userMainId' => $shopProduct->user_id,
                'imgSize' => bcdiv($uploadFile->file_size, 1024, 2),
                'imgWidth' => $uploadFile->img_width,
                'imgHeight' => $uploadFile->img_height,
                'imgExt' => $uploadFile->file_ext,
            ]));
            $ids[] = $shopImage->img_id;
        }
        ShopImageLogic::updateByWhereOfIds($where, $ids);
    }

    /**
     * handleImage
     * @param Model $shopProduct
     * @param array $where
     * @param int $k
     * @param string $v
     * @param array $ids
     * @return array
     */
    private function handleImage(Model $shopProduct, array $where, int $k, string $v, array $ids): array
    {
        $imgUrl = strlen($v) === 20 ? str_replace(config('oss.url'), '', fileIdToUrl($v)) : $v;
        $where = array_merge($where, ['imgUrl' => $imgUrl]);
        $shopImage = ShopImageLogic::findByWhere($where);
        if (is_null($shopImage)) {
            $uploadFile = UploadFileLogic::findBySerialNumber($v);
            if (is_null($uploadFile)) return $ids;
            $shopImage = ShopImageLogic::insertForModel(array_merge($where, [
                'userMainId' => $shopProduct->user_id,
                'imgSize' => bcdiv($uploadFile->file_size, 1024, 2),
                'imgWidth' => $uploadFile->img_width,
                'imgHeight' => $uploadFile->img_height,
                'imgExt' => $uploadFile->file_ext,
                'imgIsCover' => empty($ids) ? 1 : 2,
                'imgSort' => $k,
            ]));
            $ids[] = $shopImage->img_id;
        } else {
            $ids[] = $shopImage->img_id;
            if (empty($ids)) $shopImage->img_is_cover = 1;
            $shopImage->img_sort = $k;
            $shopImage->save();
        }
        return $ids;
    }
}
